<script>
    function shop_config() {
        var shop_config = JSON.parse(localStorage.getItem('config_shop'));
        if (shop_config && shop_config.shop_type !== 'undefined') {
            var shop_type = shop_config.shop_type;
            shoptype = shop_type;
            if (shop_type == 1) {        //展示
                $(".send-type").html('仅展示，不发货');
                $(".btn-group-bb,.address-select").hide();
                $(".btn-group-cc").show();
                $(".shop_tel").attr('href', 'tel:' + shop_config.shop_tel);
            } else if (shop_type == 2) {
                //自提
                $(".send-type").html('自提');
                $(".tihuo_box").show();
                $(".tihuo_address").html(shop_config.shop_address_tihuo);

                $(".wy-header-title").text('自提' + $('.wy-header-title').text())
                $(".address-select").hide();
            } else if (shop_type == 3) {
                //虚拟
                $(".send-type").html('虚拟商品发货');
                $(".wy-header-title").text('虚拟商品' + $('.wy-header-title').text())
                $(".address-select").hide();
            }
            if (shop_config.pay_wechat === 1) {
                $(".pay-wechat").show();
            }
            if (shop_config.pay_credit === 1) {
                $(".pay-credit").show();
            }
            if (shop_config.pay_alipay === 1) {
                $(".pay-alipay").show();
            }
            if (shop_config.pay_wechat === 1 && shop_config.pay_credit === 1) {
                $(".pay").addClass('btn-2')
            }
            if (shop_config.pay_wechat === 1 && shop_config.pay_credit === 1 && shop_config.pay_alipay === 1) {
                $(".pay").removeClass('btn-2').addClass('btn-3');
            }
        }
    }

    function switchCoupon() {
        var t = $(".coupon-list");
        if (t.is(':visible')) {
            t.hide();
        } else {
            t.show();
        }
    }

    function couponItem(res) {
        coupon_code = res.data.coupons.couponCode;
        let couponCount = res.data.coupons.coupon_list ? res.data.coupons.coupon_list.length : 0;
        $(".coupon-count").html(Number(couponCount));
        let citem = '';
        if (couponCount > 0) {
            $(".coupon-desc").html('，已优惠' + res.data.coupons.couponMoney + '元');
            let coupon_list = res.data.coupons.coupon_list;

            for (let i = 0; i < couponCount; i++) {
                citem += '<label class="weui-cell weui-cell_active weui-check__label" for="coupon_' + i + '">\n' +
                    '                            <div class="weui-cell__bd">\n' +
                    '                                <p><em style="color: red">省' + coupon_list[i].price + '元</em>(' + coupon_list[i].name + ')</p>\n' +
                    '                            </div>\n' +
                    '                            <div class="weui-cell__ft">\n' +
                    '                                <input type="radio" ' + (coupon_code === coupon_list[i].code ? 'checked' : '') + ' class="logCode  "  name="logCode" id="coupon_' + i + '" value="' + coupon_list[i].code + '">\n' +
                    '                                <span class="weui-icon-checked"></span>\n' +
                    '                            </div>\n' +
                    '                        </label>';
            }
        } else {
            $(".coupon-desc").html('');
        }

        $(".coupon-list-items").html(citem);
    }

    $(".coupon-list").on('click', '.logCode', function () {
        let code = $(this).val();
        if (code !== coupon_code) {
            logCode = code;
            getData();
        }
    })

    function call_pay(data) {
        switch (data.payType) {
            case 'credit':
                pay_credit(data);
                break;
            case 'wechat':
                pay_wechat(data);
                break;
            case 'alipay':
                pay_alipay(data);
                break;
        }
    }

    //创建订单
    function start_pay(type, data) {
        order_sn = data.order_sn || null;
        if (!order_sn) {
            bs_request("<{:url('/mall/order/create')}>", data, function (res) {
                if (res.code !== 0) {
                    return $.alert(res.msg);
                }
                order_sn = res.data.order_sn;
                //发起支付
                call_pay({order_sn: res.data.order_sn, payType: type})
            });
        } else {
            call_pay({order_sn: order_sn, payType: type})
        }
    }

    //余额支付
    function pay_credit(data) {
        //发起支付
        return $.confirm({
            text: "如忘记请在 <a target='_blank' href='<{:url(\'/mall/user/setting\')}>'>设置中心</a>修改密码。<br>"+'<input type="password" class="weui-input weui-prompt-input" style="padding:20px;border: 1px solid rgba(139,195,74,0.45)" id="pay-pwd" value="" placeholder="输入支付密码" />',
            title: '输入支付密码',
            autoClose: false,
            onOK: function () {
                let val = $("input[id='pay-pwd']").val()
                if (!val){
                    return $.alert('请填写支付密码')
                }
                bs_request("<{:url('/console/account/pay_pwd_check')}>",{pwd:val},function (res) {
                    if (res.code != 0){
                        return $.alert(res.msg)
                    }
                    bs_request("<{:url('/mall/order/pay')}>", data, function (ret) {
                        if (ret.code == 0) {
                            $.toast(ret.msg, function () {
                                location.href = "<{:url('/mall/order/all_orders',['status'=>'1'])}>"
                            })
                        } else {
                            return $.alert(ret.msg);
                        }
                    })

                })
            },
            onCancel: function () {
                $.toast("取消操作", "cancel");
            },

        })
        return $.confirm("确定用余额支付吗？", function () {
            bs_request("<{:url('/mall/order/pay')}>", data, function (ret) {
                if (ret.code == 0) {
                    $.toast(ret.msg, function () {
                        location.href = "<{:url('/mall/order/all_orders',['status'=>'1'])}>"
                    })
                } else {
                    return $.alert(ret.msg);
                }
            })
        })
    }

    //微信支付
    function pay_wechat(data) {
        bs_request("<{:url('/mall/order/wx_param')}>", data, function (ret) {
            if (ret.code != 0) {
                if (ret.code == 30002) {
                    $.confirm(ret.msg, function () {
                        return location.href = ret.data
                    })

                } else {
                    return $.alert(ret.msg);
                }

            } else {
                if (ret.data.payParam.mweb_url && ret.data.payParam.mweb_url !== 'undefined') {
                    return location.href = ret.data.payParam.mweb_url;
                }
                callpay(ret.data.payParam)
            }
        })
    }

    //支付宝
    function pay_alipay(data) {
        $.showLoading('火力下单中，请耐性等等~');
        bs_request("<{:url('/mall/order/alipay_param')}>", data, function (ret) {
            if (ret.code != 0) {
                $.hideLoading();
                return $.alert(ret.msg);
            } else {
                $.hideLoading();
                document.getElementById("alipay-form").innerHTML = ret.data.url;
                document.forms['alipaysubmit'].submit();
            }
        })
    }
</script>